package com.welsy.usermgt.business
{
	import mx.controls.Alert;
	import com.welsy.usermgt.vo.UserVO;
	import com.adobe.cairngorm.business.ServiceLocator;
	import mx.rpc.soap.mxml.WebService;
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.logging.Log;
	import com.welsy.usermgt.vo.ClassVO;
	import flash.xml.XMLNode;
	import mx.rpc.soap.Operation;
	import mx.core.Application;
	import com.welsy.util.NamespaceLocator;
	
	public class UpdateClassDelegate 
	{
		private var responder : IResponder;
		private var service : Object;
		
		public function UpdateClassDelegate( responder : IResponder ) {
			//Recorded in Services.mxml
			this.responder = responder;
			this.service = ServiceLocator.getInstance().getWebService("ClassroomServices") as WebService;
		}
		
		public function updateClassroom(classVO : ClassVO) : void {
			//Create the request
			
			var oper : Operation = Operation(WebService(service).getOperation("SaveClassroom"));
			var root : String = "<SaveClassroom xmlns='http://Welsy.ServiceContracts/2007/02'>";
			root += "<SaveClassroomRequest xmlns='"+NamespaceLocator.USER_MGT_DATATYPES+"'>";
			
			//Create the dynamic users part
			var users : String = "<UsersCollection>";
			var temp : String = "<UserType>";
			for each(var user : UserVO in classVO.classUsers) {
				temp += "<UserId>"+user.UserId+"</UserId>";
				temp += "<SubSystemId>"+user.SubSystemId+"</SubSystemId>";
				temp += "<RoleId>"+user.RoleId+"</RoleId>";
				temp += "<FirstName>"+user.FirstName+"</FirstName>";
				temp += "<LastName>"+user.LastName+"</LastName>";
				temp += "<Login>"+user.Login+"</Login>";
				temp += "<Email>"+user.Email+"</Email>";
				temp += "</UserType>";
				users += temp;
				
				temp = "<UserType>"; //Re-init
			}
			users += "</UsersCollection>";
			
			//Create the cloassroom properties part
			var classr : String = "<Classroom>";

			classr += "<ClassroomId>" + classVO.classId + "</ClassroomId>";
			classr += "<AuthorId>" + classVO.authorId + "</AuthorId>";
			classr += "<Title>" + classVO.className + "</Title>";
			classr += "<Description>" + classVO.classDescr + "</Description>";
			classr += "<FromDate>" + ClassVO.dateToString(classVO.durationFrom) + "</FromDate>";
			classr += "<ToDate>" + ClassVO.dateToString(classVO.durationTo) + "</ToDate>";
			classr += "<CreationDate>" + ClassVO.dateToString(classVO.creationDate) + "</CreationDate>";
			classr += "<Note>" + classVO.classNotes + "</Note>";
			classr += "</Classroom>";
			
			root += users;
			root += classr;
			root += "</SaveClassroomRequest>";
			root += "</SaveClassroom>";
			
			oper.request = new XML(root);
			var token : AsyncToken = service["SaveClassroom"].send();
			token.addResponder(responder);
			
		}
	}
}